import validate from 'wechat-validate'
Page({
  data: {
    message:'',
    code:'',
    mobile:'',
    getCodeBtnVisible:true,
    timeData: {} // 倒计时信息
  },
  behaviors: [validate],
  rules: {
    mobile: [
      {required: true, message: '手机号输入有误,请重新输入!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'}
    ],
    code: [
      { required: true, message: '验证码输入有误,请重新输入!' },
      { pattern: /^\d{6}$/, message: '请检查短信验证码是否正确!' }
    ]
  },
  getSMSCode() {
    // 获取验证结果
    const { valid, message } = this.validate('mobile')
    // 如果验证不合法则不再执行后面的逻辑
    if (!valid) return this.setData({message})
    console.log('getCode')
    this.setData({ getCodeBtnVisible: false })
    this.setData({message:''})
  },
  countDownChange(ev) {
    // console.log(ev.detail)
    this.setData({
      timeData: ev.detail,
      getCodeBtnVisible: ev.detail.minutes === 0 && ev.detail.seconds === 0,
    })
  },
  onLoad(){
    
  },
      // 2. 请求验证码
  //   const  res = await wx.$http.get('/code', { mobile: this.data.mobile })
  //   this.setData({ code : res.code })
  //     wx.$toast('验证码已发送！')
  //     // console.log(valid, message)
  //   this.setData({ getCodeBtnVisible: false })
  // },
  // countDownChange(ev) {
  //   const d = ev.detail
  //   this.setData({
  //     timeData: d,
  //     getCodeBtnVisible: ev.detail.minutes === 0 && ev.detail.seconds === 0,
  //   })
  // },
  // async submitForm(){
  //   const isAllValid = this.validate()
  //   if(!isAllValid) return
  //   const { mobile,code } = this.data
  //   const {token,refreshToken} = await wx.$http.post('/login',{mobile,code})
  //   // 拿到token,保存到本地
  //   // console.log(res)
  //   const app = getApp()
  //   // console.log(app)
  //   app.setToken('token',token)
  //   app.setToken('refreshToken',refreshToken)
  //   // 4. 跳转
  //   const url = this.redirectURL || '/pages/profile/index'
  //   wx.redirectTo({ url })
  // },
})
